package com.bdqn.controller;

import com.alibaba.fastjson.JSON;
import com.bdqn.common.Page;
import com.bdqn.pojo.RoomType;
import com.bdqn.service.RoomTypeService;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * 房型管理
 */
@Controller
@RequestMapping("/admin/room_type")
public class RoomTypeController {
    @Resource
    private RoomTypeService roomTypeService;

    /**
     * 房型管理列表页面
     * @return
     */
    @RequestMapping(value = "list",method = RequestMethod.GET)
    public ModelAndView list(ModelAndView model){
        model.setViewName("room_type/list");
        return model;
    }

    /**
     * 分页查询房型类型信息
     *
     * @param name
     * @param page
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> list(@RequestParam(name = "name", defaultValue = "") String name,
                                    @RequestParam(name = "status", required = false) Integer status, Page page) {
        Map<String, Object> ret = new HashMap<String, Object>();
        Map<String, Object> queryMap = new HashMap<String, Object>();
        queryMap.put("name", name);
        queryMap.put("status", status);
        queryMap.put("offset", page.getOffset());
        queryMap.put("pageSize", page.getRows());
        ret.put("rows", roomTypeService.findList(queryMap));
        ret.put("total", roomTypeService.getTotal(queryMap));
        return ret;
    }

    /**
     * 添加房型信息
     * @param roomType
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public Map<String,String> add(RoomType roomType){
        Map<String,String> map = new HashMap<>();
        if (roomType == null){
            map.put("type","error");
            map.put("msg","请填写正确的房型管理信息");
            return map;
        }
        if (StringUtils.isEmpty(roomType.getName())){
            map.put("type","error");
            map.put("msg","房间类型名称不能为空");
            return map;
        }
        roomType.setAvilableNum(roomType.getRoomNum()); //默认房间数等于可用房间数
        roomType.setBookNum(0);  //设置预定数为0
        roomType.setAvilableNum(0); //设置已入住数位0
        if (roomTypeService.add(roomType) <= 0){
            map.put("type","error");
            map.put("msg","添加失败");
            return map;
        }
        map.put("type", "success");
        map.put("msg", "添加成功!");
        return map;
    }

    /**
     * 删除房型信息
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/delete",method = RequestMethod.POST)
    public Map<String,String> delete(Long id){
        Map<String,String> map = new HashMap<>();
        if (id == null){
            map.put("type","error");
            map.put("msg","清选择要删除的信息");
            return map;
        }
        try{
            if (roomTypeService.delete(id) <= 0){
                map.put("type","error");
                map.put("msg","删除失败");
                return map;
            }
        }catch (Exception e){
            map.put("type", "error");
            map.put("msg", "该房间类型下存在房间信息，请先删除该房间类型下的所有房间信息!");
            return map;
        }
        map.put("type", "success");
        map.put("msg", "删除成功!");
        return map;
    }

    /**
     * 编辑房型信息
     * @param roomType
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/edit",method = RequestMethod.POST)
    public Map<String,String> edit(RoomType roomType){
        Map<String,String> map = new HashMap<>();
        if (roomType == null){
            map.put("type","error");
            map.put("msg","请填写正确的房型管理信息");
            return map;
        }
        if (StringUtils.isEmpty(roomType.getName())){
            map.put("type","error");
            map.put("msg","房间类型名称不能为空");
            return map;
        }
        RoomType existRoomType = roomTypeService.find(roomType.getId());  //获取单个房间信息
        if (existRoomType == null){
            map.put("type","error");
            map.put("msg","未找到该数据");
            return map;
        }
        Long offset = roomType.getRoomNum() - existRoomType.getRoomNum();
        roomType.setAvilableNum(existRoomType.getAvilableNum() + offset);
        if (roomType.getAvilableNum() <= 0){
            roomType.setAvilableNum(0);  //没有可用房间
            roomType.setStatus(0);  //房型已满
            if (roomType.getAvilableNum() + existRoomType.getLivedNum() + existRoomType.getBookNum() > roomType
                    .getRoomNum()){
                map.put("type","error");
                map.put("msg","房间数设置不合理");
                return map;
            }
        }
        if (roomTypeService.edit(roomType) <= 0){
            map.put("type","error");
            map.put("msg","修改失败");
            return map;
        }
        map.put("type", "success");
        map.put("msg", "修改成功!");
        return map;
    }

}
